package aom.example.security.mapper;

import aom.example.security.domain.SysRoleEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;

import java.util.List;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:41
 * @since JDK 1.8
 */
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRoleEntity> {

    /**
     * 查询用户的权限信息
     * @param uid
     * @return
     */
    @Select("select id, user_id,a.role_id,role_name,role_no,createDate from sys_user_role a LEFT JOIN sys_role b ON a.role_id=b.role_id where user_id=#{uid}")
    List<Map<String, Object>> selectRoleUserByUserId(@Param("uid")String uid);

    /**
     * 给用户添加权限
     * @param userId
     * @param roleId
     * @return
     */
    @Insert("INSERT into sys_user_role (user_id,role_id) VALUES (#{userid},#{roleid})")
    boolean insertSysUserRole(@Param("userid")String userId,@Param("roleid")String roleId);



    /**
     * 查询用户不具有的权限
     * @param uid
     * @return
     */
    @Select("select * from sys_role where role_id NOT in( " +
            " select role_id from (" +
            " select user_id,a.role_id,role_name,role_no,createDate from sys_user_role a LEFT JOIN sys_role b ON a.role_id=b.role_id where user_id=#{uid} " +
            ") a )")
    List<Map<String, Object>> selectUserNotRole(@Param("uid")String uid);


    /**
     * 删除用户与角色的关联
     * @param id
     * @return
     */
    @Delete("delete from sys_user_role where id=#{id}")
    boolean deleteSysUserRoleById(@Param("id")String id);

    /**
     * 菜单权限新增
     * @param roleId
     * @param menuId
     * @return
     */
    @Insert("INSERT INTO `springboot`.`sys_role_menu`(`role_id`, `menu_id`) VALUES (#{roleId}, #{menuId})")
    boolean insertSysRoleMenu(@Param("roleId")String roleId,@Param("menuId")String menuId);



    @Select("<script> SELECT DISTINCT(menu_id) as menu_id from sys_role_menu where  role_id in " +
            "             <foreach collection=\"ids\" item=\"item\"  close=\")\" open=\"(\" separator=\",\">" +
            "                          #{item}" +
            "              </foreach>" +
            "</script>")
    List<Map<String, Object>> selectMenuRoleByIds(@Param("ids")List<String> ids);

    @Select("select * from sys_role_menu where role_id=#{roleId} and menu_id=#{menuId}")
    Map<String, Object> selectMenuRole(@Param("roleId")String roleId,@Param("menuId")String menuId);


    @Select("<script>" +
            " select * from ( select * from ( " +
            "       select * from sys_menu where id_menu is not null and id_menu_dictory is null " +
            "   ) a LEFT JOIN (select id,role_id,menu_id as mid from sys_role_menu where role_id=#{roleid}) b ON a.menu_id=b.mid ) a " +
            "<where>" +
            "     <if test=\"keyword !=null \">" +
            "           and name like CONCAT('%',#{keyword},'%')" +
            "      </if>" +
            "</where>" +
            "order by createTime desc" +
            " limit #{page},#{size}" +
            "</script>")
    List<Map<String, Object>> selectMenuRolePage(Map map);

    @Select("<script>" +
            " select count(1) from ( select * from ( " +
            "       select * from sys_menu where id_menu is not null and id_menu_dictory is null " +
            "   ) a LEFT JOIN (select id,role_id,menu_id as mid from sys_role_menu where role_id=#{roleid}) b ON a.menu_id=b.mid ) a " +
            "<where>" +
            "     <if test=\"keyword !=null \">" +
            "           and name like CONCAT('%',#{keyword},'%')" +
            "      </if>" +
            "</where>" +
            " limit #{page},#{size}" +
            "</script>")
    Long selectMenuRoleCount(Map map);

    /**
     * 通过id删除角色与菜单的关联
     * @param id
     * @return
     */
    @Delete("delete from sys_role_menu where id=#{id}")
    boolean deleteRoleMenuById(@Param("id")Integer id);


    /**
     * 查询用户角色授权列表
     * @return
     */
    @Select("<script>" +
            "select * from (" +
            "   select * from sys_role a LEFT JOIN (select id,user_id,role_id as rid  from sys_user_role where user_id=#{userid} ) b " +
            "   on a.role_id=b.rid" +
            ") a" +
            "<where>" +
            "     <if test=\"keyword !=null \">" +
            "           or role_name like CONCAT('%',#{keyword},'%')" +
            "           or role_no like CONCAT('%',#{keyword},'%')" +
            "      </if>" +
            "</where>" +
            "   order by createDate desc limit #{page},#{size}" +
            "</script>")
    List<Map<String, Object>> pageSysUserRole(@Param("userid")String userId,@Param("keyword")String keyowrd,@Param("page")Integer page,@Param("size")Integer size);

    @Select("<script>" +
            "select count(1) from (" +
            "   select * from sys_role a LEFT JOIN (select id,user_id,role_id as rid  from sys_user_role where user_id=#{userid} ) b " +
            "   on a.role_id=b.rid" +
            ") a" +
            "<where>" +
            "     <if test=\"keyword !=null \">" +
            "           or role_name like CONCAT('%',#{keyword},'%')" +
            "           or role_no like CONCAT('%',#{keyword},'%')" +
            "      </if>" +
            "</where>" +
            "</script>")
    Long countSysUserRole(@Param("userid")String userId,@Param("keyword")String keyowrd);


    /**
     * 删除与roleId相关的所有菜单
     * @param roleId
     * @return
     */
    @Delete("delete from sys_role_menu where role_id=#{roleId}")
    boolean deleteSysRoleMenuByRoleId(@Param("roleId") String roleId);

}
